<?php

namespace Admin\Controller;

class PurchaseController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Purchase");
    }

    //首页
    public function index() {
        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["ps.enterprise_code"] = $user["enterprise_code"];
        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);
//print_r($list);
//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("user", $user);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function indexOther() {
        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["ps.enterprise_code"] = $user["enterprise_code"];
        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);
//print_r($list);
//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("user", $user);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function purchaseMaterial() {
        $id = $_REQUEST["id"];
        $info = D("Purchase")->queryById($id);

        $users_obj = M("admin");
        $userId = $_SESSION['ADMIN_ID'];
        $user = $users_obj->where("user_id=$userId")->find();

        if(isNullOrEmpty($info)){
            $enterprise = D("Enterprise")->where(array('enterprise_code'=>$user["enterprise_code"]))->find();
            $info["purchase_no"] = $enterprise["workorder_no_head"] . "WLCG-".date('Ymd',time()) . sprintf("%02d", substr(D("Purchase")->count(), -2, 2)+1);
            $info["update_id"] = $user["employee_id"];
        }

        $map["purchase_no"] = $info["purchase_no"];
        $list = D("PurchaseMaterial")->queryForList(null, $map);

        $listCode = D('Code')->alias("c")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->join("left join __MATERIAL__ m on m.code_id = c.code_id")
            ->where(array('c.level' => 1, 'c.enterprise_code'=>$user["enterprise_code"]))
            ->field("c.*, c.name as code_name, p.name as parent_name, concat('【', p.name, '】', c.name) as full_name, m.material_name, m.spec, m.prop, m.unit, m.barcode")
            ->select();

        $this->assign('listCode', $listCode);
        $this->assign("info", $info);
        $this->assign("user", $user);
        $this->assign("list", $list);
        $this->assign("row", count($list));
        $this->display("purchaseMaterial");
    }

    /**
     * 添加数据
     */
    public function purchaseMaterialPost() {

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $enterprise = D("Enterprise")->where(array('enterprise_code'=>$user["enterprise_code"]))->find();

        $id = $_REQUEST["id"];
        $data = D("Purchase")->queryById($id);
        if($_REQUEST["btnText"] == "入库"){
            $purchase_no = $enterprise["workorder_no_head"] . "WLCG-".date('Ymd',time()) . sprintf("%02d", substr(D("Purchase")->count(), -2, 2)+1);
            if(isNullOrEmpty($id)){
                $data["purchase_no"] = $purchase_no;
                $data["enterprise_code"] = $user["enterprise_code"];
                $data["purchase_date"] = $_REQUEST["purchase_date"];
                $data["create_time"] = getCurrentDate();
                $data["type"] = "物料";
                $data["purchase_id"] = $this->currentModel->add($data);
            }else{
                $purchase_no = $data["purchase_no"];
            }

            if(isNullOrEmpty($data["purchase_date"]))
                $data["purchase_date"] = getCurrentDate();
            $data["store_id"] = $_REQUEST["store_id"];
            $data["supplier_id"] = $_REQUEST["supplier_id"];
            $data["remark"] = $_REQUEST["remark"];
            $data["update_id"] = $user["employee_id"];
            $data["purchaser_id"] = $user["employee_id"];
            $data["update_time"] = getCurrentDate();
            $data["status"] = -1;
            $this->currentModel->updateInfo($data);

            $oldList = D("PurchaseMaterial")->where(array('purchase_no'=>$data["purchase_no"]))->select();

            $row = $_REQUEST["row"];
            $idArr = array();
            for($i = 0; $i < $row; $i++){
                if(isNullOrEmpty($_REQUEST["code_id_".$i])){
                    continue;
                }
                $tmpData = null;
                $tmpData["purchase_material_id"] = $_REQUEST["purchase_material_id_".$i];
                $tmpData["purchase_no"] = $purchase_no;
                $tmpData["code_id"] = $_REQUEST["code_id_".$i];
                $tmpData["batch_no"] = $_REQUEST["batch_no_".$i];
                $tmpData["number"] = $_REQUEST["number_".$i];
                $tmpData["price"] = $_REQUEST["price_".$i];
                $tmpData["production_date"] = $_REQUEST["production_date_".$i];
                if(isNullOrEmpty($tmpData["production_date"]))
                    $tmpData["production_date"] = getCurrentDate();
                $tmpData["expiry_date"] = $_REQUEST["expiry_date_".$i];
                if(isNullOrEmpty($tmpData["expiry_date"]))
                    $tmpData["expiry_date"] = getCurrentDate();
                $tmpData["total_price"] = $_REQUEST["total_price_".$i];
                $tmpData["update_time"] = getCurrentDate();
                $tmpData["update_id"] = $user["employee_id"];
                if(isNullOrEmpty($tmpData["purchase_material_id"])){
                    $tmpData["create_time"] = getCurrentDate();
                    $tmpData["enterprise_code"] = $user["enterprise_code"];
                    D("PurchaseMaterial")->add($tmpData);
                }else{
                    D("PurchaseMaterial")->updateInfo($tmpData);
                    array_push($idArr, $tmpData["purchase_material_id"]);
                }
            }

            foreach ($oldList as $old){
                if (!in_array($old["purchase_material_id"], $idArr)) {
                    D("PurchaseMaterial")->deleteById($old["purchase_material_id"]);
                }
            }
            $this->success("采购入库成功，单号：".$purchase_no, U("Purchase/index"));
        }
        if($_REQUEST["btnText"] == "审核"){
            $data["status"] = 1;
            $data["auditor_id"] = $user["employee_id"];
            $data["update_time"] = getCurrentDate();
            $this->currentModel->updateInfo($data);

            $List = D("PurchaseMaterial")->where(array('purchase_no'=>$data["purchase_no"]))->select();
            foreach ($List as $tmpData){
                $tmpData["store_id"] = $data["store_id"];
                $tmpData["supplier_id"] = $data["supplier_id"];
                $tmpData["type"] = "物料";
                $tmpStorage = D("Storage")
                    ->where(array('type'=>'物料',
                        'enterprise_code'=>$user["enterprise_code"],
                        'code_id'=>$tmpData["code_id"],
                        'store_id'=>$tmpData["store_id"],
                        'supplier_id'=>$tmpData["supplier_id"],
//                        'price'=>$tmpData["price"],
                        'batch_no'=>$tmpData["batch_no"]))
                    ->find();
                if(isNullOrEmpty($tmpStorage)){
                    $tmpData["storage_number"] = $tmpData["number"];
                    D("Storage")->add($tmpData);
                }else{
                    $totalPrice = ($tmpStorage["price"] * $tmpStorage["storage_number"]) + ($tmpData["price"] * $tmpData["number"]);
                    $tmpStorage["storage_number"] += $tmpData["number"];
                    $tmpStorage["price"] = $totalPrice / $tmpStorage["storage_number"];
                    $tmpStorage["update_time"] = getCurrentDate();
                    $tmpStorage["update_id"] = $user["employee_id"];
                    D("Storage")->updateInfo($tmpStorage);
                }

                //计算平均成本
                $allItemList = D("Storage")
                    ->where(array('type'=>'物料',
                        'enterprise_code'=>$user["enterprise_code"],
                        'code_id'=>$tmpData["code_id"],
                        'store_id'=>$tmpData["store_id"],
                        ))
                    ->select();
                $totalItemPrice = 0;
                $totalItemNumber = 0;
                foreach ($allItemList as $tmpItem){
                    $totalItemPrice += ($tmpItem["price"] * $tmpItem["storage_number"]);
                    $totalItemNumber += $tmpItem["storage_number"];
                }
                $averagePrice = $totalItemPrice/$totalItemNumber;
                foreach ($allItemList as $tmpItem){
                    $tmpItem["price"] = sprintf("%.5f", $averagePrice);
                    D("Storage")->updateInfo($tmpItem);
                }
            }



            $this->success("审核成功，单号：".$data["purchase_no"], U("Purchase/index"));
        }
        if($_REQUEST["btnText"] == "反审核"){
            $data["status"] = -2;
            $data["auditor_id"] = $user["employee_id"];
            $data["update_time"] = getCurrentDate();
            $this->currentModel->updateInfo($data);

            $List = D("PurchaseMaterial")->where(array('purchase_no'=>$data["purchase_no"]))->select();
            foreach ($List as $tmpData){
                $tmpData["store_id"] = $data["store_id"];
                $tmpData["supplier_id"] = $data["supplier_id"];
                $tmpData["type"] = "物料";
                $tmpStorage = D("Storage")
                    ->where(array('type'=>'物料',
                        'enterprise_code'=>$user["enterprise_code"],
                        'code_id'=>$tmpData["code_id"],
                        'store_id'=>$tmpData["store_id"],
                        'supplier_id'=>$tmpData["supplier_id"],
//                        'price'=>$tmpData["price"],
                        'batch_no'=>$tmpData["batch_no"]))
                    ->find();
                if(!isNullOrEmpty($tmpStorage)){
                    if($tmpStorage["storage_number"] == $tmpData["number"]){
                        D("Storage")->where(array("storage_id" => $tmpStorage["storage_id"]))->delete();
                    }else{
//                        $totalPrice = ($tmpStorage["price"] * $tmpStorage["storage_number"]) - ($tmpData["price"] * $tmpData["number"]);
                        $tmpStorage["storage_number"] -= $tmpData["number"];
//                        $tmpStorage["price"] = $totalPrice / $tmpStorage["storage_number"];
                        $tmpStorage["update_time"] = getCurrentDate();
                        $tmpStorage["update_id"] = $user["employee_id"];
                        D("Storage")->updateInfo($tmpStorage);
                    }
                }else{
                    $tmpData["store_id"] = $data["store_id"];
                    $tmpData["supplier_id"] = $data["supplier_id"];
                    $tmpData["type"] = "物料";
                    $tmpData["storage_number"] *= -1;
                    $tmpData["create_time"] = getCurrentDate();
                    $tmpData["update_time"] = getCurrentDate();
                    $tmpData["update_id"] = $user["employee_id"];
                    $tmpData["storage_number"] = $tmpData["number"];
                    D("Storage")->add($tmpData);
                }

                //计算平均成本
                $allItemList = D("Storage")
                    ->where(array('type'=>'物料',
                        'enterprise_code'=>$user["enterprise_code"],
                        'code_id'=>$tmpData["code_id"],
                        'store_id'=>$tmpData["store_id"],
                    ))
                    ->select();
                $totalItemPrice = ($tmpStorage["price"] - $tmpData["price"]) * $tmpData["number"] ;
                $totalItemNumber = 0;
                foreach ($allItemList as $tmpItem){
                    $totalItemPrice += ($tmpItem["price"] * $tmpItem["storage_number"]);
                    $totalItemNumber += $tmpItem["storage_number"];
                }
                $averagePrice = $totalItemPrice/$totalItemNumber;
                foreach ($allItemList as $tmpItem){
                    $tmpItem["price"] = sprintf("%.5f", $averagePrice);
                    D("Storage")->updateInfo($tmpItem);
                }
            }

            $this->success("反审核成功，单号：".$data["purchase_no"], U("Purchase/index"));
        }
    }

    public function purchaseFinishedProduct() {
        $id = $_REQUEST["id"];
        $info = D("Purchase")->queryById($id);

        $users_obj = M("admin");
        $userId = $_SESSION['ADMIN_ID'];
        $user = $users_obj->where("user_id=$userId")->find();

        if(isNullOrEmpty($info)){
            $enterprise = D("Enterprise")->where(array('enterprise_code'=>$user["enterprise_code"]))->find();
            $info["purchase_no"] = $enterprise["workorder_no_head"] . "CPCG-".date('Ymd',time()) . sprintf("%02d", substr(D("Purchase")->count(), -2, 2)+1);
            $info["update_id"] = $user["employee_id"];
        }

        $map["purchase_no"] = $info["purchase_no"];
        $list = D("PurchaseFinishedProduct")->queryForList(null, $map);

        $listFinishedProduct = D('FinishedProduct')->alias("fp")
            ->join("left join __CLASS__ cl on cl.class_id = fp.class_id")
            ->join("left join __SUPPLIER__ sl on sl.supplier_id = fp.supplier_id")
            ->where(array('fp.enterprise_code'=>$user["enterprise_code"]))
            ->order("fp.finished_product_id asc")
            ->field("fp.*, cl.class_name, sl.supplier_name")
            ->select();

        $this->assign('listFinishedProduct', $listFinishedProduct);
        $this->assign("info", $info);
        $this->assign("user", $user);
        $this->assign("list", $list);
        $this->assign("row", count($list));
        $this->display("purchaseFinishedProduct");
    }

    public function purchaseFinishedProductPost() {

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $enterprise = D("Enterprise")->where(array('enterprise_code'=>$user["enterprise_code"]))->find();

        $id = $_REQUEST["id"];
        $data = D("Purchase")->queryById($id);
        if($_REQUEST["btnText"] == "入库"){
            $purchase_no = $enterprise["workorder_no_head"] . "CPCG-".date('Ymd',time()) . sprintf("%02d", substr(D("Purchase")->count(), -2, 2)+1);
            if(isNullOrEmpty($id)){
                $data["purchase_no"] = $purchase_no;
                $data["enterprise_code"] = $user["enterprise_code"];
                $data["purchase_date"] = $_REQUEST["purchase_date"];
                $data["create_time"] = getCurrentDate();
                $data["type"] = "成品矫形器";
                $data["purchase_id"] = $this->currentModel->add($data);
            }else{
                $purchase_no = $data["purchase_no"];
            }

            if(isNullOrEmpty($data["purchase_date"]))
                $data["purchase_date"] = getCurrentDate();
            $data["store_id"] = $_REQUEST["store_id"];
//            $data["supplier_id"] = $_REQUEST["supplier_id"];
            $data["remark"] = $_REQUEST["remark"];
            $data["update_id"] = $user["employee_id"];
            $data["purchaser_id"] = $user["employee_id"];
            $data["update_time"] = getCurrentDate();
            $data["status"] = -1;
            $this->currentModel->updateInfo($data);

            $oldList = D("PurchaseFinishedProduct")->where(array('purchase_no'=>$data["purchase_no"]))->select();

            $row = $_REQUEST["row"];
            $idArr = array();
            for($i = 0; $i < $row; $i++){
                if(isNullOrEmpty($_REQUEST["finished_product_id_".$i])){
                    continue;
                }
                $tmpData = null;
                $tmpData["purchase_finished_product_id"] = $_REQUEST["purchase_finished_product_id_".$i];
                $tmpData["purchase_no"] = $purchase_no;
                $tmpData["finished_product_id"] = $_REQUEST["finished_product_id_".$i];
                $tmpData["size"] = $_REQUEST["size_".$i];
                $tmpData["supplier_id"] = $_REQUEST["supplier_id_".$i];
                $tmpData["batch_no"] = $_REQUEST["batch_no_".$i];
                $tmpData["number"] = $_REQUEST["number_".$i];
                $tmpData["price"] = $_REQUEST["price_".$i];
                $tmpData["production_date"] = $_REQUEST["production_date_".$i];
                if(isNullOrEmpty($tmpData["production_date"]))
                    $tmpData["production_date"] = getCurrentDate();
                $tmpData["expiry_date"] = $_REQUEST["expiry_date_".$i];
                if(isNullOrEmpty($tmpData["expiry_date"]))
                    $tmpData["expiry_date"] = getCurrentDate();
                $tmpData["total_price"] = $_REQUEST["total_price_".$i];
                $tmpData["update_time"] = getCurrentDate();
                $tmpData["update_id"] = $user["employee_id"];
                if(isNullOrEmpty($tmpData["purchase_finished_product_id"])){
                    $tmpData["create_time"] = getCurrentDate();
                    $tmpData["enterprise_code"] = $user["enterprise_code"];
                    D("PurchaseFinishedProduct")->add($tmpData);
                }else{
                    D("PurchaseFinishedProduct")->updateInfo($tmpData);
                    array_push($idArr, $tmpData["purchase_finished_product_id"]);
                }
            }

            foreach ($oldList as $old){
                if (!in_array($old["purchase_finished_product_id"], $idArr)) {
                    D("PurchaseFinishedProduct")->deleteById($old["purchase_finished_product_id"]);
                }
            }
            $this->success("采购入库成功，单号：".$purchase_no, U("Purchase/index"));
        }
        if($_REQUEST["btnText"] == "审核"){
            $data["status"] = 1;
            $data["auditor_id"] = $user["employee_id"];
            $data["update_time"] = getCurrentDate();
            $this->currentModel->updateInfo($data);

            $List = D("PurchaseFinishedProduct")->where(array('purchase_no'=>$data["purchase_no"]))->select();
            foreach ($List as $tmpData){
                $tmpData["store_id"] = $data["store_id"];
//                $tmpData["supplier_id"] = $data["supplier_id"];
                $tmpData["type"] = "成品矫形器";
                $tmpStorage = D("Storage")
                    ->where(array('type'=>'成品矫形器',
                        'enterprise_code'=>$user["enterprise_code"],
                        'finished_product_id'=>$tmpData["finished_product_id"],
                        'store_id'=>$tmpData["store_id"],
                        'size'=>$tmpData["size"],
//                        'supplier_id'=>$tmpData["supplier_id"],
//                        'price'=>$tmpData["price"],
                        'batch_no'=>$tmpData["batch_no"]))
                    ->find();
                if(isNullOrEmpty($tmpStorage)){
                    $tmpData["storage_number"] = $tmpData["number"];
//                    $tmpData["code_id"] = $tmpData["finished_product_id"];
                    D("Storage")->add($tmpData);
                }else{
                    $totalPrice = ($tmpStorage["price"] * $tmpStorage["storage_number"]) + ($tmpData["price"] * $tmpData["number"]);
                    $tmpStorage["storage_number"] += $tmpData["number"];
                    $tmpStorage["price"] = $totalPrice / $tmpStorage["storage_number"];
                    $tmpStorage["update_time"] = getCurrentDate();
                    $tmpStorage["update_id"] = $user["employee_id"];
                    D("Storage")->updateInfo($tmpStorage);
                }
                //计算平均成本
                $allItemList = D("Storage")
                    ->where(array('type'=>'成品矫形器',
                        'enterprise_code'=>$user["enterprise_code"],
                        'finished_product_id'=>$tmpData["finished_product_id"],
                        'store_id'=>$tmpData["store_id"],
                    ))
                    ->select();
                $totalItemPrice = 0;
                $totalItemNumber = 0;
                foreach ($allItemList as $tmpItem){
                    $totalItemPrice += ($tmpItem["price"] * $tmpItem["storage_number"]);
                    $totalItemNumber += $tmpItem["storage_number"];
                }
                $averagePrice = $totalItemPrice / $totalItemNumber;
                foreach ($allItemList as $tmpItem) {
                    $tmpItem["price"] = sprintf("%.5f", $averagePrice);
                    D("Storage")->updateInfo($tmpItem);
                }
            }

            $this->success("审核成功，单号：".$data["purchase_no"], U("Purchase/index"));
        }
        if($_REQUEST["btnText"] == "反审核"){
            $data["status"] = -2;
            $data["auditor_id"] = $user["employee_id"];
            $data["update_time"] = getCurrentDate();
            $this->currentModel->updateInfo($data);

            $List = D("PurchaseFinishedProduct")->where(array('purchase_no'=>$data["purchase_no"]))->select();
            foreach ($List as $tmpData){
                $tmpData["store_id"] = $data["store_id"];
                $tmpData["type"] = "成品矫形器";
                $tmpStorage = D("Storage")
                    ->where(array('type'=>'成品矫形器',
                        'enterprise_code'=>$user["enterprise_code"],
                        'finished_product_id'=>$tmpData["finished_product_id"],
                        'store_id'=>$tmpData["store_id"],
                        'size'=>$tmpData["size"],
//                        'supplier_id'=>$tmpData["supplier_id"],
//                        'price'=>$tmpData["price"],
                        'batch_no'=>$tmpData["batch_no"]))
                    ->find();
                if(!isNullOrEmpty($tmpStorage)){
                    if($tmpStorage["storage_number"] == $tmpData["number"]){
                        D("Storage")->where(array("storage_id" => $tmpStorage["storage_id"]))->delete();
                    }else{
//                        $totalPrice = ($tmpStorage["price"] * $tmpStorage["storage_number"]) - ($tmpData["price"] * $tmpData["number"]);
                        $tmpStorage["storage_number"] -= $tmpData["number"];
//                        $tmpStorage["price"] = $totalPrice / $tmpStorage["storage_number"];
                        $tmpStorage["update_time"] = getCurrentDate();
                        $tmpStorage["update_id"] = $user["employee_id"];
                        D("Storage")->updateInfo($tmpStorage);
                    }
                }else{
                    $tmpData["store_id"] = $data["store_id"];
//                    $tmpData["code_id"] = $tmpData["finished_product_id"];
                    $tmpData["supplier_id"] = $data["supplier_id"];
                    $tmpData["type"] = "成品矫形器";
                    $tmpData["storage_number"] *= -1;
                    $tmpData["create_time"] = getCurrentDate();
                    $tmpData["update_time"] = getCurrentDate();
                    $tmpData["update_id"] = $user["employee_id"];
                    $tmpData["storage_number"] = $tmpData["number"];
                    D("Storage")->add($tmpData);
                }

                //计算平均成本
                $allItemList = D("Storage")
                    ->where(array('type'=>'成品矫形器',
                        'enterprise_code'=>$user["enterprise_code"],
                        'finished_product_id'=>$tmpData["finished_product_id"],
                        'store_id'=>$tmpData["store_id"],
                    ))
                    ->select();
                $totalItemPrice = ($tmpStorage["price"] - $tmpData["price"]) * $tmpData["number"] ;
                $totalItemNumber = 0;
                foreach ($allItemList as $tmpItem){
                    $totalItemPrice += ($tmpItem["price"] * $tmpItem["storage_number"]);
                    $totalItemNumber += $tmpItem["storage_number"];
                }
                $averagePrice = $totalItemPrice/$totalItemNumber;
                foreach ($allItemList as $tmpItem){
                    $tmpItem["price"] = sprintf("%.5f", $averagePrice);
                    D("Storage")->updateInfo($tmpItem);
                }
            }

            $this->success("反审核成功，单号：".$data["purchase_no"], U("Purchase/index"));
        }
    }

    public function importData() {
        $data["purchase_no"] = $_REQUEST["purchase_no"];
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["status"] = -1;
        $data["enterprise_code"] = $user["enterprise_code"];
        $data["purchase_date"] = getCurrentDate();
        $data["create_time"] = getCurrentDate();
        $data["update_time"] = getCurrentDate();
        $data["update_id"] = $user["employee_id"];
        $data["purchaser_id"] = $user["employee_id"];
        $data["type"] = $_REQUEST["type"];
        $data["store_id"] = $_REQUEST["store_id"];
        $data["supplier_id"] = $_REQUEST["supplier_id"];
        $data["remark"] = "Excel导入数据";
        $this->currentModel->add($data);
        $this->success("导入成功，单号：".$data["purchase_no"], U("Purchase/index"));
    }

    public function getTable()
    {
        $ids = $_REQUEST["id"];
        $map['class_name'] = "reporting_purchase";
        $formDict = D("FormDict")->queryForList($map);

        $data = array();
        $data["header"] = array();
        $data["volume"] = array();
        $data["list"] = array();
        foreach ($formDict as $key => $value){
            array_push($data["header"], $value);
            array_push($data["volume"], $key);
        }

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        //获取列表
        $list = D("Purchase")->alias("ps")
            ->join("left join __ADMIN__ emp on emp.employee_id = ps.update_id")
            ->join("left join __ADMIN__ auditor on auditor.employee_id = ps.auditor_id")
            ->join("left join __ADMIN__ purchaser on purchaser.employee_id = ps.purchaser_id")
            ->join("left join __STORE__ s on s.store_id = ps.store_id")
            ->join("left join __SUPPLIER__ sp on sp.supplier_id = ps.supplier_id")
            ->where(array('ps.enterprise_code'=>$user["enterprise_code"]))
            ->order("ps.update_time desc")
            ->field("ps.*, emp.admin_name, auditor.admin_name as auditor, purchaser.admin_name as purchaser, s.store_name, sp.supplier_name")
            ->select();

//        $ids = $_REQUEST["id"];
        $idArr = explode(',', $ids);
        foreach ($list as $item){
            if(in_array($item["purchase_id"], $idArr)){
                switch ($item["status"]){
                    case '-2' :
                        $item["status"] = "已反审";
                        break;
                    case '-1' :
                        $item["status"] = "待审核";
                        break;
                    case '1' :
                        $item["status"] = "已审核";
                        break;
                }
                if($item["type"] == "成品矫形器"){
                    $orderList = D("PurchaseFinishedProduct")->alias("psfp")
                        ->join("left join __ADMIN__ emp on emp.employee_id = psfp.update_id")
                        ->join("left join __FINISHED_PRODUCT__ fp on fp.finished_product_id = psfp.finished_product_id")
                        ->join("left join __CLASS__ cl on cl.class_id = fp.class_id")
                        ->join("left join __SUPPLIER__ sl on sl.supplier_id = fp.supplier_id")
                        ->where(array('psfp.purchase_no'=>$item["purchase_no"]))
                        ->order("psfp.purchase_finished_product_id asc")
                        ->field("psfp.*, emp.admin_name, fp.name, fp.class_id, fp.unit, sl.supplier_name, fp.image_url, cl.class_name")
                        ->select();
                    foreach ($orderList as $order){
                        $order["purchaser"] = $item["purchaser"];
                        $order["purchase_date"] = $item["purchase_date"];
                        $order["auditor"] = $item["auditor"];
                        $order["audit_time"] = $item["audit_time"];
                        $order["purchaser"] = $item["purchaser"];
                        $order["status"] = $item["status"];
                        $order["type"] = "成品矫形器";
                        $order["code"] = $order["finished_product_code"];
                        $order["name"] = $order["finished_product_name"];
                        $order["spec"] = $order["size"];
                        $order["prop"] = $order["class_name"];
                        $order["barcode"] = $order["finished_product_barcode"];
                        $order["unit"] = $order["finished_product_unit"];
                        array_push($data["list"], $order);
                    }
                }
                if($item["type"] == "物料"){
                    $orderList = D("PurchaseMaterial")->alias("psm")
                        ->join("left join __ADMIN__ emp on emp.employee_id = psm.update_id")
                        ->join("left join __CODE__ c on c.code_id = psm.code_id")
                        ->join("left join __CODE__ p on p.code_id = c.parentid")
                        ->join("left join __MATERIAL__ m on m.code_id = psm.code_id")
                        ->where(array('psm.purchase_no'=>$item["purchase_no"]))
                        ->order("psm.purchase_material_id asc")
                        ->field("psm.*, emp.admin_name, c.name as code_name, p.name as parent_name, m.material_name, m.spec, m.prop, m.unit, m.barcode")
                        ->select();
                    foreach ($orderList as $order){
                        $order["purchaser"] = $item["purchaser"];
                        $order["purchase_date"] = $item["purchase_date"];
                        $order["auditor"] = $item["auditor"];
                        $order["audit_time"] = $item["audit_time"];
                        $order["status"] = $item["status"];
                        $order["type"] = "物料";
                        $order["code"] = $order["parent_name"].".".$order["code_name"];
                        $order["name"] = $order["material_name"];
                        $order["barcode"] = $order["material_barcode"];
                        $order["unit"] = $order["material_unit"];
                        array_push($data["list"], $order);
                    }
                }
            }
        }
//        $fileName = "定制产品报表_".date('Ymd',time());
        $fileName = "采购报表_";
        $fileName .= "_" . date('Ymd',time());
        exportExcel($data, $fileName);
    }

    public function remove() {

        $id = $_REQUEST['id'];
        $this->currentModel->deleteById($id);

        $this->success('删除成功!', U("Storage/index"));

    }
    
}